﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Xna.Framework;

namespace BlobTowerDefense.Tools
{
    public class Curve3D
    {
        private Curve curveX;
        private Curve curveY;
        private Curve curveZ;

        public Curve3D(Vector3 positionStart, Vector3 positionEnd)
        {
            curveX = new Curve();
            curveY = new Curve();
            curveZ = new Curve();

            curveX.Keys.Add(new CurveKey(0.0f, positionStart.X));
            curveY.Keys.Add(new CurveKey(0.0f, positionStart.Y));
            curveZ.Keys.Add(new CurveKey(0.0f, positionStart.Z));

            curveX.Keys.Add(new CurveKey(1.0f, positionEnd.X));
            curveY.Keys.Add(new CurveKey(1.0f, positionEnd.Y));
            curveZ.Keys.Add(new CurveKey(1.0f, positionEnd.Z));
        }

        public Curve3D(Vector3 positionStart, Vector3 positionEnd, float timeStart, float timeEnd)
        {
            curveX = new Curve();
            curveY = new Curve();
            curveZ = new Curve();

            curveX.Keys.Add(new CurveKey(timeStart, positionStart.X));
            curveY.Keys.Add(new CurveKey(timeStart, positionStart.Y));
            curveZ.Keys.Add(new CurveKey(timeStart, positionStart.Z));

            curveX.Keys.Add(new CurveKey(timeEnd, positionEnd.X));
            curveY.Keys.Add(new CurveKey(timeEnd, positionEnd.Y));
            curveZ.Keys.Add(new CurveKey(timeEnd, positionEnd.Z));
        }

        public Curve3D(float[] timesOfPositions, Vector3[] positions, int level)
        {
            curveX = new Curve();
            curveY = new Curve();
            curveZ = new Curve();

            if (timesOfPositions.Length != positions.Length)
            {
                throw new ArgumentException("arrays in arguments needs the same length");
            }

            for (int i = 0; i < timesOfPositions.Length; i++)
            {
                curveX.Keys.Add(new CurveKey(timesOfPositions[i], positions[i].X));
                curveY.Keys.Add(new CurveKey(timesOfPositions[i], positions[i].Y));
                curveZ.Keys.Add(new CurveKey(timesOfPositions[i], positions[i].Z));
            }

            if (level == 1)
            {
                //for smooth motion of enemies
                curveX.ComputeTangents(CurveTangent.Smooth);
                curveY.ComputeTangents(CurveTangent.Smooth);
                curveZ.ComputeTangents(CurveTangent.Smooth);
            }
            else if (level == 2)
            {
                curveX.ComputeTangents(CurveTangent.Linear);
                curveY.ComputeTangents(CurveTangent.Linear);
                curveZ.ComputeTangents(CurveTangent.Linear);
            }
            
        }

        public Vector3 GetPosition(float normalizedTime)
        {
            return new Vector3(curveX.Evaluate(normalizedTime), curveY.Evaluate(normalizedTime), curveZ.Evaluate(normalizedTime));
        }

        public float GetStartTime()
        {
            return curveX.Keys[0].Position;
        }

        public float GetEndTime()
        {
            return curveX.Keys[curveX.Keys.Count - 1].Position;
        }
    }
}
